import sys
from bs4 import BeautifulSoup
import urllib.request as urllib2
import urllib
import re
import os
import save_sm_mid_imgs as saveIm
import saveToDB as DB
from datetime import datetime



print('---------------------- start download -------------------------------')
print(str(datetime.now()))
url = 'http://www.doorstore.co.uk/products/internal-interior-doors/oak-doors/641/'

#url = 'http://www.doorstore.co.uk/products/internal-interior-doors/mahogany-doors/630/'

page_from = '630'

base_url = 'http://www.doorstore.co.uk'

response = urllib2.urlopen(url)
html = response.read()

soup = BeautifulSoup(html)

img_classes = soup.findAll('ul', {'class': 'pro-list'})

for im in img_classes:
	elems = im.findAll('a')
		
	for eli in elems:
		src = eli.get('href')

		#print('src= ' + src)
		img =  eli.findNext('img')
		src2 = img.get('src')

		item_name = img.get('alt')

		print('src2= ' + src2)		
		
		folder = re.search('(((\d)|(-){1})+)$', src).group(0)
		#print(folder)

		newpath = r'doors_photos\\' + folder
		#newpath = folder
		if not os.path.exists(newpath): os.makedirs(newpath)
		

		smal_imgs_url = base_url + src
		
		

		#---------------------------------

		
		if not os.path.exists(newpath): os.makedirs(newpath)
		
		#print(base_url)

		url = base_url + src2

		url = url.replace(' ','%20')

		filenamemid = src2.replace('/ProdImages/','').replace('/prodimages/','')

		image_on_web = urllib2.urlopen(url)
		buf = image_on_web.read()

		path_to_file = newpath + '\\'+filenamemid

		#print('path_to_file=' + path_to_file)
		downloaded_image = open(path_to_file, "wb")
		downloaded_image.write(buf)
		downloaded_image.close()
		image_on_web.close()

		image_type = 'catalog_image'

		photo_name = filenamemid
		folder = 'doors_photos'+ '/' + folder
		photo_type = 'catalog_image'
		item_name  = item_name
		catalog_photo_id = DB.savePhotoToBd(photo_name, folder, photo_type, item_name, page_from)

		print("-- catalog_photo_id = " + catalog_photo_id)		


		saveIm.save(smal_imgs_url, newpath,item_name,page_from,catalog_photo_id)


		#print("url =" + smal_imgs_url)
		#print("new_path = " + newpath)


print('---------------------- start download -------------------------------')
print(str(datetime.now()))